package com.ruoyi.bank.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bank.mapper.BankGreyRegisterMapper;
import com.ruoyi.bank.domain.BankGreyRegister;
import com.ruoyi.bank.service.IBankGreyRegisterService;

import javax.validation.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 灰名册管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-15
 */
@Service
public class BankGreyRegisterServiceImpl implements IBankGreyRegisterService 
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Autowired
    private BankGreyRegisterMapper bankGreyRegisterMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询灰名册管理
     * 
     * @param customerId 灰名册管理主键
     * @return 灰名册管理
     */
    @Override

    public BankGreyRegister selectBankGreyRegisterByCustomerId(Long customerId)
    {
        return bankGreyRegisterMapper.selectBankGreyRegisterByCustomerId(customerId);
    }
    /**
     * 查询灰名册管理
     *
     * @param customerNumber 灰名册管理主键
     * @return 灰名册管理
     */
    @Override
    public BankGreyRegister selectBankGreyRegisterByCustomerNumber(String customerNumber)
    {
        return bankGreyRegisterMapper.selectBankGreyRegisterByCustomerNumber(customerNumber);
    }
    /**
     * 导入用户数据
     *
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    /**
     * 导入
     */

    @Override
    public String importGrey(List<BankGreyRegister> greyList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(greyList) || greyList.isEmpty()) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BankGreyRegister bankGreyRegister : greyList) {
            try {
                //检查必填项是否有空项
                if(bankGreyRegister.getCustomerName() == "" || bankGreyRegister.getCustomerNumber() == "" || bankGreyRegister.getCustomerPhonenumber() == ""){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + ". "+" 该条数据必填项未填写完整");
                    continue; // 跳过当前循环，处理下一条记录
                }
                // 新增检查客户身份证号长度是否为18位
                if (bankGreyRegister.getCustomerNumber().length() != 18) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + ". "+bankGreyRegister.getCustomerName()+"身份证号 " + bankGreyRegister.getCustomerNumber() + " 非法，身份证号必须为18位");
                    continue; // 跳过当前循环，处理下一条记录
                }
                // 新增检查手机号长度是否为11位
                if (bankGreyRegister.getCustomerPhonenumber().length() != 11) {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + ". "+bankGreyRegister.getCustomerName()+"身份证号 " + bankGreyRegister.getCustomerNumber() + " 的手机号非法，手机号必须为11位");
                    continue; // 跳过当前循环，处理下一条记录
                }
                // 验证是否存在这个客户编号
                BankGreyRegister existingBankGreyRegister = bankGreyRegisterMapper.selectBankGreyRegisterByCustomerNumber(bankGreyRegister.getCustomerNumber());
                if (StringUtils.isNull(existingBankGreyRegister)) {
                    BeanValidators.validateWithException(validator, bankGreyRegister);
                    bankGreyRegister.setCreateBy(operName);
                    bankGreyRegisterMapper.insertBankGreyRegister(bankGreyRegister);
                    successNum++;
                    successMsg.append("<br/>" + successNum +". "+bankGreyRegister.getCustomerName()+"身份证号 " + bankGreyRegister.getCustomerNumber() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, bankGreyRegister);
                    bankGreyRegister.setUpdateBy(operName);
                    bankGreyRegisterMapper.updateBankGreyRegister(bankGreyRegister);
                    successNum++;
                    successMsg.append("<br/>" + successNum + ". "+bankGreyRegister.getCustomerName()+"身份证号 " + bankGreyRegister.getCustomerNumber() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + ". "+bankGreyRegister.getCustomerName()+"身份证号 " + bankGreyRegister.getCustomerNumber() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + ". "+bankGreyRegister.getCustomerName()+"身份证号 " + bankGreyRegister.getCustomerNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            return failureMsg.toString(); // 修改此处以返回错误消息
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
            return successMsg.toString();
        }
    }

    /**
     * 查询灰名册管理列表
     * 
     * @param bankGreyRegister 灰名册管理
     * @return 灰名册管理
     */
    @Override
    public List<BankGreyRegister> selectBankGreyRegisterList(BankGreyRegister bankGreyRegister)
    {
        return bankGreyRegisterMapper.selectBankGreyRegisterList(bankGreyRegister);
    }

    /**
     * 新增灰名册管理
     * 
     * @param bankGreyRegister 灰名册管理
     * @return 结果
     */
    @Override
    public int insertBankGreyRegister(BankGreyRegister bankGreyRegister)
    {
        return bankGreyRegisterMapper.insertBankGreyRegister(bankGreyRegister);
    }

    /**
     * 修改灰名册管理
     * 
     * @param bankGreyRegister 灰名册管理
     * @return 结果
     */
    @Override
    public int updateBankGreyRegister(BankGreyRegister bankGreyRegister)
    {
        return bankGreyRegisterMapper.updateBankGreyRegister(bankGreyRegister);
    }

    /**
     * 批量删除灰名册管理
     * 
     * @param customerIds 需要删除的灰名册管理主键
     * @return 结果
     */
    @Override
    public int deleteBankGreyRegisterByCustomerIds(Long[] customerIds)
    {
        return bankGreyRegisterMapper.deleteBankGreyRegisterByCustomerIds(customerIds);
    }

    /**
     * 删除灰名册管理信息
     * 
     * @param customerId 灰名册管理主键
     * @return 结果
     */
    @Override
    public int deleteBankGreyRegisterByCustomerId(Long customerId)
    {
        return bankGreyRegisterMapper.deleteBankGreyRegisterByCustomerId(customerId);
    }
}
